"""
https://github.com/offu/WeRoBot/issues/486
腾讯的限制
[BUG] handle里面函数处理超过5秒会连续调用3次
"""

import werobot
import datetime
import sys
from flask_test import text_gen

robot = werobot.WeRoBot(token='kx8M7eODU4HiyAmw3a')
robot.config['APP_ID'] = 'wx47577a029b258f6b'
robot.config['ENCODING_AES_KEY'] = 'LjBrnwSvqGXx89Tszleo4sMYp8yMALcTArwhPjxoZGJ'


@robot.handler
def hello(message):
    
    xtype = message.type
    print(datetime.datetime.now(), xtype)
    if 'text' != xtype:
        return '目前仅支持文本信息！'
    
    xinput = message.content.strip()
    print('input:', xinput)
    xlen = len(xinput)
    xoutput = text_gen(xinput)
    print('output:', xoutput)
    return xoutput[:200]

# 让服务器监听在 0.0.0.0:80
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()
